package cc.wsyw126.java.nowcoder.sword_offer;

/**
 * Created by Administrator on 2017/4/17.
 */
public class FindNumsAppearOnce {
    public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {
        if (array == null || array.length == 0) return;
        int num = 0;
        for (int i = 0; i < array.length; i++) {
            num^=array[i];
        }
        int indexOf1 = findFirstBitIs1(num);

        int one = 0, two = 0;
        for (int i = 0; i < array.length; i++) {
            if (isBit1(array[i], indexOf1)) {
                one^=array[i];
            }else {
                two^=array[i];
            }
        }
        num1[0]=one;
        num2[0]=two;
    }

    private boolean isBit1(int num, int indexOf1) {
        num = num >> indexOf1;
        if ((num&0x1) == 1) {
            return true;
        }
        return false;
    }

    private int findFirstBitIs1(int num) {
        int index = 0;
        while ((num &0x1) == 0 && index < Integer.SIZE){
            num=num>>1;
            index++;
        }
        return index;
    }

    public static void main(String[] args) {
        FindNumsAppearOnce findNumsAppearOnce = new FindNumsAppearOnce();
        int[] one= new int[1];
        int[] two= new int[1];
        findNumsAppearOnce.FindNumsAppearOnce(new int[]{2,2,3,5,4,3,6,6},one, two );
        System.out.println(one[0]);
        System.out.println(two[0]);
    }
}
